<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-if="checkPermission(permissionRoles.shop_list_download)" :loading="downloadLoading" type="primary" icon="el-icon-download" class="filter-item" @click="handleDownload()">导出</el-button>
      <selectServer @server-selected="handleSelectServer" />
      <el-select v-model="searchForm.shop_id" class="filter-item" style="width: 110px;" clearable :loading="loading" placeholder="选择商店">
        <el-option
          v-for="item in shops"
          :key="item.shop_id"
          :label="item.shop_name"
          :value="item.shop_id"
        >
        </el-option>
      </el-select>
      <el-date-picker v-model="searchForm.startDate" class="filter-item" type="date" style="width: 160px;" placeholder="开始时间"></el-date-picker>-
      <el-date-picker v-model="searchForm.endDate" class="filter-item" type="date" style="width: 160px;" placeholder="结束时间"></el-date-picker>
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" class="mt-10" border height="710">
      <el-table-column label="排序" prop="index" width="110px" align="center"></el-table-column>
      <el-table-column label="商店" prop="shop_name" width="130px" align="center"></el-table-column>
      <el-table-column label="商品" prop="item_name" width="130px" align="center"></el-table-column>
      <el-table-column label="购买人数" prop="num" width="130px" align="center"></el-table-column>
      <el-table-column label="购买次数" prop="times" width="130px" align="center"></el-table-column>
      <el-table-column label="消耗货币" prop="currency" width="98px" align="center"></el-table-column>
      <el-table-column label="消耗货币数量" prop="amount" width="98px" align="center"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import SelectServer from '@/components/SelectServer'
import { getShopList, getShopData } from '@/api/statis'
import checkPermission from '@/utils/permission'

export default {
  name: 'ShopList',
  components: { SelectServer },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      list: [],
      shops: [],
      loading: false,
      listLoading: false,
      downloadLoading: false,
      searchForm: {
        shop_id: '',
        servers: [],
        startDate: new Date(Date.now() - 86400000 * 7),
        endDate: new Date()
      }
    }
  },
  created() {
    this.getList()
    this.getShopData()
  },
  methods: {
    checkPermission,
    handleFilter() {
      this.getList()
    },
    handleSelectServer(servers) {
      this.searchForm.servers = servers
    },
    async getList() {
      this.listLoading = true
      const res = await getShopList(this.searchForm)
      this.listLoading = false
      this.list = res.data
    },
    async getShopData() {
      const res = await getShopData()
      this.shops = res.data
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['排序', '商店', '商品', '购买人数', '购买次数', '消耗货币数量']
        const filterVal = ['index', 'shop_name', 'item_name', 'num', 'times', 'amount']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '商店数据'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>
